/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TuyenBayFrm.java
 *
 * Created on May 27, 2011, 10:19:03 AM
 */

package gui;

import dao.TuyenBayDAO;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.TuyenBayPOJO;

/**
 *
 * @author b2
 */
public class TuyenBayFrm extends javax.swing.JFrame {

    /** Creates new form TuyenBayFrm */
    public TuyenBayFrm() {
        initComponents();
    }
    public void hienThi(){
         ArrayList<TuyenBayPOJO> ds = TuyenBayDAO.LayDanhSachTuyenBay();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã tuyến bay");
        dtm.addColumn("Sân bay đi");
        dtm.addColumn("Sân bay đến");
        dtm.setNumRows(ds.size());
        for(int i = 0; i < ds.size(); i ++)
        {
            TuyenBayPOJO cb = ds.get(i);
            dtm.setValueAt(cb.getMaTuyenBay(), i, 0);
            dtm.setValueAt(cb.getSanBayDi(), i, 1);
            dtm.setValueAt(cb.getSanBayDen(), i, 2);
        }
        this.jtbQuanLyTuyenBay.setModel(dtm);
        this.jtbQuanLyTuyenBay.changeSelection(0, 0, false, false);
        this.txtMaTuyenBay.setText("");

    }
    public void timkiemTuyenBay(){
        String maTuyenBay = this.txtMaTuyenBay.getText();
         ArrayList<TuyenBayPOJO> ds = TuyenBayDAO.TimKiemTyenBayTheoTen(maTuyenBay);
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã tuyến bay");
        dtm.addColumn("Sân bay đi");
        dtm.addColumn("Sân bay đến");
        dtm.setNumRows(ds.size());
        for(int i = 0; i < ds.size(); i ++)
        {
            TuyenBayPOJO cb = ds.get(i);
            dtm.setValueAt(cb.getMaTuyenBay(), i, 0);
            dtm.setValueAt(cb.getSanBayDi(), i, 1);
            dtm.setValueAt(cb.getSanBayDen(), i, 2);
        }
        this.jtbQuanLyTuyenBay.setModel(dtm);
        this.jtbQuanLyTuyenBay.changeSelection(0, 0, false, false);

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbQuanLyTuyenBay = new javax.swing.JTable();
        btnThoat = new javax.swing.JButton();
        btnThemMoi = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnCapNhat = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtMaTuyenBay = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý tuyến bay");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("QUẢN LÝ TUYẾN BAY");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Quản lý tuyến bay", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jtbQuanLyTuyenBay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbQuanLyTuyenBay);

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnThemMoi.setText("Thêm mới");
        btnThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemMoiActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(btnCapNhat)
                        .addGap(18, 18, 18)
                        .addComponent(btnThemMoi)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnXoa)
                    .addComponent(btnThemMoi)
                    .addComponent(btnCapNhat))
                .addContainerGap())
        );

        jLabel2.setText("Mã tuyến bay");

        txtMaTuyenBay.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMaTuyenBayCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(178, 178, 178)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(jLabel2)
                        .addGap(64, 64, 64)
                        .addComponent(txtMaTuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMaTuyenBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.hienThi();
    }//GEN-LAST:event_formWindowOpened
 public void xoaTuyenBay() {
        int dong = this.jtbQuanLyTuyenBay.getSelectedRow();
        String maSanBay = (String) this.jtbQuanLyTuyenBay.getValueAt(dong, 0);
        boolean kq = TuyenBayDAO.xoaTuyenBay(maSanBay);
        if (kq == true) {
            JOptionPane.showMessageDialog(this, "Xóa thành công");
            //this.TimKiem();
            this.hienThi();
        } else {
            JOptionPane.showMessageDialog(this, "Xóa không thành công");
        }

    }
    private void btnThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemMoiActionPerformed
        // TODO add your handling code here:
        ThemTuyenBayFrm frm = new ThemTuyenBayFrm();
        frm.setVisible(true);
    }//GEN-LAST:event_btnThemMoiActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        this.xoaTuyenBay();
    }//GEN-LAST:event_btnXoaActionPerformed

    private void txtMaTuyenBayCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMaTuyenBayCaretUpdate
        // TODO add your handling code here:
        this.timkiemTuyenBay();
    }//GEN-LAST:event_txtMaTuyenBayCaretUpdate

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
        this.hienThi();
    }//GEN-LAST:event_btnCapNhatActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TuyenBayFrm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbQuanLyTuyenBay;
    private javax.swing.JTextField txtMaTuyenBay;
    // End of variables declaration//GEN-END:variables

}
